package com.example.hospital.service.impl;


import com.example.hospital.mapper.LeaveMapper;
import com.example.hospital.mapper.UserMapper;
import com.example.hospital.pojo.LeaveRequest;
import com.example.hospital.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveMapper leaveMapper;

    @Autowired
    private UserMapper userMapper;

    // 提交请假申请
    @Override
    public void applyLeave(LeaveRequest leaveRequest) {
        leaveRequest.setStatus("待审批");
        leaveMapper.insertLeave(leaveRequest);
    }

    // 获取单个用户的请假申请
    @Override
    public List<LeaveRequest> getLeavesByUserId(Integer userId) {
        return leaveMapper.findByUserId(userId);
    }

    // 获取所有请假申请
    @Override
    public List<LeaveRequest> getAllLeaves() {
        return leaveMapper.findAll();
    }

    // 审批请假申请
    @Override
    public void approveLeave(Integer leaveId) {
        LeaveRequest leave = leaveMapper.findById(leaveId);
        if (leave != null && leave.getStatus().equals("待审批")) {
            leave.setStatus("已批准");
            leaveMapper.updateLeave(leave);
        }
    }

    //拒绝审批
    @Override
    public void rejectLeave(Integer leaveId) {
        LeaveRequest leave = leaveMapper.findById(leaveId);
        if (leave != null && leave.getStatus().equals("待审批")) {
            leave.setStatus("已拒绝");
            leaveMapper.updateLeave(leave);
        }
    }
}
